<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
        title="个人信息"
        left-arrow
        @click-left="$router.back()"
    />

    <input type="file" 
        hidden 
        ref="file"
        @change="onFileChange"
        >

    <!-- 个人信息 -->
    <van-cell title="头像" is-link @click="$refs.file.click()">
        <van-image 
            class="avatar"
            fit="cover"
            round
            :src="user.photo"
        />
    </van-cell>
    <van-cell title="昵称" :value="user.name" is-link @click="(isUserNameShow = true)"></van-cell>
    <van-cell title="性别" :value="(user.gender ===1 )? '女' : '男'" is-link @click="(isUserSexShow = true)"></van-cell>
    <van-cell title="生日" :value="user.birthday" is-link @click="(isUserBirthdayShow = true)"></van-cell>

    <!-- 修改信息\\昵称 -->
    <van-popup style="height:100%" 
        v-model='isUserNameShow'
        position="bottom"
    >
    <commentName 
        v-if="isUserNameShow"
        @close="isUserNameShow = false"
        v-model="user.name"/>
    </van-popup>

    <!-- 修改性别 -->
    <van-popup
        v-model='isUserSexShow'
        position="bottom"
    >
    <commentSex
        v-if="isUserSexShow"
        @close="isUserSexShow = false"
        v-model="user.gender"
    />
    </van-popup>

    <!-- 修改生日 -->
    <van-popup
        v-model='isUserBirthdayShow'
        position="bottom"
    >
    <commentBirthday
        v-if="isUserBirthdayShow"
        @close="isUserBirthdayShow = false"
        v-model="user.birthday"
    />
    </van-popup>

    <!-- 编辑头像 -->
    <van-popup style="height:100%" 
        v-model='isUserPhotoShow'
        position="bottom"
    >
    <commentPhoto
        v-if="isUserPhotoShow"
        @close="isUserPhotoShow = false"
        :img="img"
        @upPhoto="user.photo = $event"
        />
    </van-popup>
  </div>
</template>

<script>
import {getUserProfile} from '@/api/user'
import commentName from './component/comment-name.vue'
import commentSex from './component/comment-sex.vue'
import commentBirthday from './component/comment-birthday.vue'
import commentPhoto from './component/comment-photo.vue'
export default {
    name:'UserProfile',
    data() {
        return {
            user:{}, //用户个人信息
            isUserNameShow:false,
            isUserSexShow:false,
            isUserBirthdayShow:false,
            isUserPhotoShow:false,
            img:null
        }
    },
    components:{
        commentName,
        commentSex,
        commentBirthday,
        commentPhoto
    },
    created(){
        this.loaduser()
    },
    methods:{
        async loaduser(){
            try{
                const {data} = await getUserProfile()
                this.user = data.data
            }catch(err){
                this.$toast('获取数据失败')
            }
        },
        onFileChange(){
            //读取文件对象
            const file = this.$refs.file.files[0]
            //基于文章对象获取blob数据
            const data = window.URL.createObjectURL(file)
            this.img = data

            //展示图片弹出层
            this.isUserPhotoShow = true
            //file-inout如果选了同一个文件不会触发change事件
            //解决办法：清空value
            this.$refs.file.value = ''
        }
    }

}
</script>

<style lang="less" scoped>
.user-profile{
    .avatar{
        width: 60px;
        height: 60px;
    }
    .van-popup{
        background-color: #f5f7f9;
    }
}
    
</style>